home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************\
-
- File: v hex.c
-
- Purpose: This module handles hex numbers as strings and strings as
- hex numbers.
-
-
- Voyeur -- a no-frills file viewer
- Copyright ©1993-4, Mark Pilgrim
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program in a file named "GNU General Public License".
- If not, write to the Free Software Foundation, 675 Mass Ave,
- Cambridge, MA 02139, USA.
-
- \**********************************************************************/
-
- #include "v hex.h"
- #include "program globals.h"
-
- void HexStringToAsciiString(Str255 hexString, Str255 asciiString)
- {
- int i;
-
- if (hexString[0]&1)
- {
- hexString[hexString[0]+1]=hexString[hexString[0]];
- hexString[hexString[0]]='0';
- hexString[0]++;
- }
- asciiString[0]=0x00;
- for (i=1; i<=hexString[0]; i+=2)
- asciiString[++asciiString[0]]=
- ((hexString[i]-((hexString[i]>'9') ? 'A' : '0'))<<4) +
- hexString[i+1]-((hexString[i+1]>'9') ? 'A' : '0');
- }
-
- void LongToHexString(unsigned long input, Str255 result)
- {
- result[0]=0x08;
- result[1]=hexchar[0][(input>>24)&0xff];
- result[2]=hexchar[1][(input>>24)&0xff];
- result[3]=hexchar[0][(input>>16)&0xff];
- result[4]=hexchar[1][(input>>16)&0xff];
- result[5]=hexchar[0][(input>>8)&0xff];
- result[6]=hexchar[1][(input>>8)&0xff];
- result[7]=hexchar[0][input&0xff];
- result[8]=hexchar[1][input&0xff];
- }
-
- Boolean ValidHex(Str255 tempStr)
- {
- int i;
-
- for (i=1; i<=tempStr[0]; i++)
- if (!(((tempStr[i]>='0') && (tempStr[i]<='9')) ||
- (((tempStr[i]|0x20)>='a') && ((tempStr[i]|0x20)<='f'))))
- return FALSE;
-
- return TRUE;
- }
-
- unsigned long HexStringToLong(Str255 tempStr)
- {
- int i;
- unsigned long result;
- unsigned long mult;
- unsigned long digit;
-
- while (tempStr[0]<8)
- {
- for (i=tempStr[0]; i>0; i--)
- tempStr[i+1]=tempStr[i];
- tempStr[1]='0';
- tempStr[0]++;
- }
-
- mult=1L;
- result=0L;
- for (i=8; i>0; i--)
- {
- if ((tempStr[i]>='0') && (tempStr[i]<='9'))
- digit=tempStr[i]-'0';
- else
- digit=(tempStr[i]|0x20)-'a'+10;
- result+=digit*mult;
- mult=mult<<4;
- }
-
- return result;
- }
-
- pascal Boolean HexOFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
- {
- unsigned char theChar;
- short itemType;
- Handle itemH;
- Rect box;
- unsigned long dummy;
-
- switch (theEvent->what)
- {
- case keyDown:
- case autoKey:
- theChar=theEvent->message & charCodeMask;
- if ((theChar==0x0d) || (theChar==0x03))
- {
- *theItem=1;
- GetDItem(theDialog, 1, &itemType, &itemH, &box);
- HiliteControl((ControlHandle)itemH, 1);
- Delay(8, &dummy);
- HiliteControl((ControlHandle)itemH, 0);
- return TRUE;
- }
- if ((theChar==0x1b) ||
- ((theEvent->modifiers & cmdKey) && (theChar=='.')))
- {
- *theItem=2;
- GetDItem(theDialog, 2, &itemType, &itemH, &box);
- HiliteControl((ControlHandle)itemH, 1);
- Delay(8, &dummy);
- HiliteControl((ControlHandle)itemH, 0);
- return TRUE;
- }
- if ((theChar>='a') && (theChar<='f'))
- theEvent->message&=0xffffffdf; /* convert to uppercase */
- else if ((theChar>='A') && (theChar<='F'));
- else if ((theChar>='0') && (theChar<='9'));
- else if ((theChar==0x08) || (theChar==0x09) ||
- ((theChar>=0x1c) && (theChar<=0x1f)));
- else if (theEvent->modifiers&cmdKey);
- else
- {
- SysBeep(7);
- theEvent->what=nullEvent;
- }
- break;
- }
-
- return FALSE;
- }
-